
#include <stdlib.h>
#include <stdio.h>
#include <taskLib.h>
#include <msgQLib.h>
#include <semLib.h>
#include <time.h>

#include "simulation.h"
#include "tConditionnement.h"
#include "tMiseEnPalette.h"
#include "tStockage.h"
#include "tExpedition.h"
#include "types.h"
#include "tProduction.h"
#include "tJournal.h"
#include "tLog.h"
#include "tMere.h"
#include "config.h"
#include "macros.h"
#include "tcpServer.h"
#include "tcpClient.h"

Entrepot entrepot;
MSG_Q_ID balPalette;
MSG_Q_ID balCarton;
MSG_Q_ID balOrdreCond;
MSG_Q_ID balOrdreExpe;
MSG_Q_ID balMessageLog;
MSG_Q_ID balJournal;

pid_t carteTache[NB_TACHES];

SEM_ID semCapteurDim;
int shmCapteurDim;


int numCond = 0;
int numCommande = 0;


void Init()
{
	srand(time(NULL));
    remove(FICHIER_LOG);
	
	balPalette = msgQCreate(MAXMSG_BALPALETTE, sizeof(MsgPalette), MSG_Q_FIFO);
	balCarton = msgQCreate(MAXMSG_BALCARTON, sizeof(MsgCarton), MSG_Q_FIFO);
	balOrdreCond = msgQCreate(MAXMSG_BALORDRECOND, sizeof(MsgOrdreCond)+100, MSG_Q_FIFO);
	balOrdreExpe = msgQCreate(MAXMSG_BALORDREEXPE, sizeof(MsgOrdreExpe), MSG_Q_FIFO);
	balMessageLog = msgQCreate(MAXMSG_BALMESSAGELOG, sizeof(Trame), MSG_Q_FIFO);
	balJournal = msgQCreate(MAXMSG_BALMESSAGELOG, TAILLEMSG_BALMESSAGELOG, MSG_Q_FIFO);
	balMsgRecv = msgQCreate(MAXMSG_BALMESSAGELOG, sizeof(Trame), MSG_Q_FIFO);
	balMsgEnv = msgQCreate(MAXMSG_BALMESSAGELOG, sizeof(Trame), MSG_Q_FIFO);
	
	entrepot = NULL;
	semCapteurDim = semBCreate(SEM_Q_FIFO, SEM_EMPTY);
	shmCapteurDim = -1;
	
	carteTache[T_RECEPTORDRE] = taskSpawn("envoi", 92, 0, 100, (FUNCPTR) tcpClient, "localhost", 0, 0, 0, 0, 0, 0, 0, 0, 0);
	carteTache[T_ENVOI] = taskSpawn("envoi", 92, 0, 100, (FUNCPTR) tcpServer, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
	carteTache[T_CONDITIONNEMENT] = taskSpawn("conditionnement", 95, 0, 100,  (FUNCPTR) tConditionnement, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
	carteTache[T_MISEENPALETTE] = taskSpawn("miseEnPalette", 96, 0, 100, (FUNCPTR) tMiseEnPalette, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
	carteTache[T_STOCKAGE] = taskSpawn("stockage", 97, 0, 100, (FUNCPTR) tStockage, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
	carteTache[T_EXPEDITION] = taskSpawn("expedition", 98, 0, 100, (FUNCPTR) tExpedition, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
	carteTache[T_PRODUCTION] = taskSpawn("production", 94, 0, 100, (FUNCPTR) tProduction, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
	carteTache[T_JOURNAL] = taskSpawn("journal", 91, 0, 100, (FUNCPTR) tJournal, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
	carteTache[T_LOG] = taskSpawn("log", 99, 0, 100, (FUNCPTR) tLog, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}


void Cond(int typePiece, int quantite)
{
	MsgOrdreCond * msgOrdreCond;
	
	msgOrdreCond = (MsgOrdreCond *) malloc(sizeof(MsgOrdreCond));
	msgOrdreCond->num = numCond += 1;
	msgOrdreCond->type = typePiece;
	msgOrdreCond->quantite = quantite;
	
	msgQSend(balOrdreCond, (char *) msgOrdreCond, sizeof(MsgOrdreCond), WAIT_FOREVER, MSG_PRI_NORMAL);
	
	free(msgOrdreCond);
}

void viewFile(char * fileName, int chmode)
{
    int file; // Fichier a lire
    int lineSize = 255;
    char aChar;

    file = open(fileName, O_RDONLY, chmode);
    
    if (file == NULL)
    {
        printf("Impossible d'ouvrir le fichier.");
    }
    else
    {
    	while (read(file, &aChar, 1) != 0)
        {
    		printf("%c", aChar);
        }
    }
    close(file);
}

void vw()
{
	viewFile(FICHIER_LOG, FILEMODE);
}

void ATrame(char c, char * c2)
{
	int i = 0;
	Trame * trame;
	char cent[TAILLEMSG_BALMESSAGELOG];
	trame = (Trame *) malloc(sizeof(Trame));
	
	//printf("sizeof(Trame) = %d , %d", sizeof(Trame), TAILLEMSG_BALMESSAGELOG);
	
	trame->lenght = 21+strlen(c2);
	/*for(i=0;i<TAILLEMSG_BALMESSAGELOG;i++)
	{
		trame->content[i] = ' ';
	}*/
	trame->content[0] = c;
	for(i=0;i<20;i++)
	{
		trame->content[i+1] = 't';
	}
	
	for(i=0;i<strlen(c2);i++)
	{
		trame->content[i+21] = c2[i];
	}
	
	msgQSend(balMsgRecv, (char *) trame, TAILLEMSG_BALMESSAGELOG, WAIT_FOREVER, MSG_PRI_NORMAL);
	
	free(trame);
}

void Expe(int quantite1, int quantite2)
{
	MsgOrdreExpe * msgOrdreExpe;
	
	msgOrdreExpe = (MsgOrdreExpe *) malloc(sizeof(MsgOrdreExpe));
	msgOrdreExpe->numCommande = numCommande += 1;
	msgOrdreExpe->quai = randBornes(1, 15);
	msgOrdreExpe->quantite1 = quantite1;
	msgOrdreExpe->quantite2 = quantite2;
	
	msgQSend(balOrdreExpe, (char *) msgOrdreExpe, sizeof(MsgOrdreExpe), WAIT_FOREVER, MSG_PRI_NORMAL);
	
	free(msgOrdreExpe);
}

void EndTest()
{
	semDelete(semCapteurDim);

	DBG("Fin de supression Sem.\n");
	
	taskDelete(carteTache[T_CONDITIONNEMENT]);
	taskDelete(carteTache[T_MISEENPALETTE]);
	taskDelete(carteTache[T_STOCKAGE]);
	taskDelete(carteTache[T_EXPEDITION]);
	taskDelete(carteTache[T_PRODUCTION]);
	taskDelete(carteTache[T_JOURNAL]);
	taskDelete(carteTache[T_LOG]);
	taskDelete(carteTache[T_ENVOI]);
	taskDelete(carteTache[T_RECEPTORDRE]);
	
	DBG("Fin de supression Tache.\n");
	
	msgQDelete(balPalette);
	msgQDelete(balCarton);
	msgQDelete(balOrdreCond);
	msgQDelete(balOrdreExpe);
	msgQDelete(balMessageLog);
	msgQDelete(balJournal);
	msgQDelete(balMsgRecv);
	msgQDelete(balMsgEnv);

	DBG("Fin de supression Bal.\n");
}

void testProduction()
{
	int i =0;

	Init();

	ATrame('I',"L00005P00005C00005\n");
	ATrame('C',"L00001A00001B00001\n");
	ATrame('A',"\n");

	//EndTest();
}


